﻿
using Shinva.LADModel.Abstractions;
using Shinva.LADModel.Models;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shinva.LADModel.Utilities
{

    public static  class DiagramTraversal 
    {




        /// <summary>
        /// 遍历树，获取路径数组
        /// </summary>
        /// <param name="tree"></param>
        /// <returns></returns>
        public static List<string> GetPathArray(Diagram nodeTree)
        {
            List<string> pathArray = new List<string>();
            //网络的遍历
            for (int i = 0; i < nodeTree.Networks.Count; i++)
            {
                pathArray.AddRange(GetPathHelper(nodeTree.Networks[i]));
            }
            return pathArray;
        }

        private static List<string> GetPathHelper(IGraphNode node, List<string> result = null)
        {
            if (result == null)
            {
                result = new List<string>() { "Header" }; //初始化自定义一个头部数据
            }
            //基线条件，如果节点的下一个节点为空并且是一个路径节点，说明找到末尾了
            if (node==null)
            {
                return result;
            }
            //递归条件，判断节点的类型
            //1 如果节点是虚拟节点类型
            if (node.NodeType == NodeType.VirtualNode && node is VirtualNode vn)
            {
                ArrayHelper(result, vn.Name);
                //第一次进入的时候走这里，因为设定的Root节点为虚拟节点


                if (vn.enableLines.Count > 1)
                {
                    List<string> temp = new List<string>();
                    for (int j = 0; j < vn.enableLines.Count; j++)
                    {
                        temp.AddRange(GetPathHelper(vn.enableLines[j].ToNode, new List<string>(result)));
                    }
                    return temp;
                }
                else
                { //这里是只有一个子节点的情况
                    if (vn.enableLines.Count > 0)
                    {
                    return GetPathHelper(vn.enableLines[0].ToNode, result);

                    }
                    else
                    {
                        throw new Exception("最后一个元素不能放虚拟节点");
                    }
                }

            }
            //2 如果节点是路径节点类型 现在没有pathNode了
            //else if (node.NodeType == NodeType.PathNode && node is PathNodeBase pn)
            //{
            //    ArrayHelper(result, pn.Name);
            //    return GetPathHelper(pn.To, result);
            //}
            //3 如果节点是元器件节点类型
            else if (node.NodeType == NodeType.ElementNode && node is ElementNodeBase en)
            {
                ArrayHelper(result, en.GetType().Name+"("+en.Name+")");
                return GetPathHelper(en.enoLine.ToNode, result);
            }
            else if (node.NodeType == NodeType.NetNode && node is Powerrail nd)
            {
                ArrayHelper(result, "NetNode");
                List<string> temp = new List<string>();
                for (int j = 0; j < nd.enableLines.Count; j++)
                {
                    temp.AddRange(GetPathHelper(nd.enableLines[j].ToNode, new List<string>(result)));
                }
                return temp;
            }
            else 
            {
                throw new Exception("遍历路径异常");
            }

        }

        private static List<string> ArrayHelper(List<string> result, string str)
        {
            for (int i = 0; i < result.Count; i++)
            {
                result[i] += "++" + str;
            }
            return result;
        }
    }
}
